<?
require_once(dirname(__FILE__) . '/../../classes/module_interface.inc.php');
require_once(dirname(__FILE__) . '/config.inc.php');

class LoggerModuleDay implements LoggerModuleInterface
{
    protected $database;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
    }

    public function PrepareParseSession()
    {
        return true;
    }

    public function ParseHit(&$hit)
    {
        if (isset($hit['modules']['robots']))
            return false;

        $time       = '\'' . $hit['source']['time'] . '\'';
        $visitor    = intval($hit['source']['visitor']);
        $session    = intval($hit['source']['session']);

        $this->database->Query('LOCK TABLES `[PREF]module_day` WRITE, `[PREF]module_day_archive` WRITE');

        $newVisitor  = false;
        $SelectSQL = 'SELECT `date` FROM `[PREF]module_day` WHERE `visitor`='.$visitor.' AND `session`!='.$session.' LIMIT 0,1';
        $result = $this->database->Query($SelectSQL);
        if (!$result)
            trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
        if ($this->database->NumRows($result) == 0)
        {
            $newVisitor = true;
    
            $SelectSQL = 'SELECT `date` FROM `[PREF]module_day_archive` WHERE `visitor`='.$visitor.' LIMIT 0,1';
            $result = $this->database->Query($SelectSQL);
            if (!$result)
                trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
            if ($this->database->NumRows($result) > 0)
                $newVisitor = false;
        }

        $UpdateSQL = 'UPDATE `[PREF]module_day` SET `hits`=`hits`+1 ';
        $UpdateSQL .= 'WHERE `date`=DATE('.$time.') AND `visitor`='.$visitor.' AND `session`='.$session;
        $result = $this->database->Query($UpdateSQL);
        if (!$result)
            trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        if ($this->database->AffectedRows() == 0)
        {
            $InsertSQL = 'INSERT INTO `[PREF]module_day` (`date`, `visitor`, `new`, `session`) ';
            $InsertSQL .= 'VALUES('.$time.', '.$visitor.', '.intval($newVisitor).', '.$session.')';
            $result = @$this->database->Query($InsertSQL);
        }

        $this->database->Query('UNLOCK TABLES');
        
        $hit['modules']['day']['newVisitor'] = $newVisitor;

        return true;
    }

    public function Archive()
    {
        $date = LoggerModuleDayConfig::GetArchiveDate();
        $dateEscaped = '\''.mysql_escape_string($date).'\'';

        $this->database->Query('LOCK TABLES `[PREF]module_day` WRITE, `[PREF]module_day_archive` WRITE');

        $ArchiveSQL = 'INSERT INTO `[PREF]module_day_archive` (`date`, `visitor`, `new`, `session`, `hits`) ';
        $ArchiveSQL .= 'SELECT `date`, `visitor`, `new`, `session`, `hits` FROM `[PREF]module_day` WHERE `date`<'.$dateEscaped;
        $this->database->Query($ArchiveSQL);
        $DeleteSQL = 'DELETE FROM `[PREF]module_day` WHERE `date`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);

        $this->database->Query('UNLOCK TABLES');

        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_day`';
        $this->database->Query($OptimizeSQL);
        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_day_archive`';
        $this->database->Query($OptimizeSQL);
    }

    public function Purge()
    {
        $date = LoggerModuleDayConfig::GetPurgeDate();
        if ($date != '1970-01-01')
        {
            $dateEscaped = '\''.mysql_escape_string($date).'\'';
    
            $DeleteSQL = 'DELETE FROM `[PREF]module_day` WHERE `time`<'.$dateEscaped;
            $this->database->Query($DeleteSQL);
            $DeleteSQL = 'DELETE FROM `[PREF]module_day_archive` WHERE `date`<'.$dateEscaped;
            $this->database->Query($DeleteSQL);
        }

        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_day`';
        $this->database->Query($OptimizeSQL);
        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_day_archive`';
        $this->database->Query($OptimizeSQL);

        return true;
    }
}